/*
 * Copyright (c) 2009 Johnny Mast
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#import "Scenes.h"

@interface Scenes (Private)
	- (id) init;
	- (void) dealloc;
@end

@implementation Scenes
@synthesize _nextScene;
@synthesize _ActiveScene;
@synthesize _Scenes;


@synthesize currentlyBoundTexture;
@synthesize currentGameState;
@synthesize globalAlpha;

static Scenes* ScenesObject;

+ (Scenes *)SceneManager {
	@synchronized(self)
	{
		if (ScenesObject == nil)
		{
			ScenesObject = [[self alloc] init];
		}
	}
	return ScenesObject;
}


- (id) init {
	[super init];
	_Scenes   = [[NSMutableDictionary alloc] initWithCapacity:10];
	globalAlpha = 1; // NOTE !
	return self;
}


-(void) flushScenes {
	@synchronized(self)
	{
		[_Scenes release];
		[self init];
	}
}

-(void)setSceneForKey:(NSString*)sceneKey Scene:(GameScene*)Scene {
	[_Scenes setObject:Scene forKey:sceneKey];	
}

-(GameScene*)getSceneForKey:(NSString*)sceneKey {
	
	if (!sceneKey) return nil;
	return [_Scenes objectForKey:sceneKey];
}

-(void) setActiveScene:(NSString*)sceneKey {
	_ActiveScene = [self getSceneForKey:sceneKey];
	
	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 40)];
	[[UIAccelerometer sharedAccelerometer] setDelegate:_ActiveScene];			

}

-(GameScene*) getActiveScene {
	return _ActiveScene;	
}

-(void)setNextScene:(NSString *)nextScene {
	_nextScene = nextScene;
}

-(NSString*)getNextScene {
	if (_nextScene != nil)
	{
		NSString* tmp = [[NSString alloc] initWithString:_nextScene];
		_nextScene = nil;
		return tmp;
	}
	return nil;
}



// From here ported for backwards compatability -- NOT OFFICIALY POART OF XForce
- (void)addSceneWithKey:(NSString*)aSceneKey scene:(GameScene*)aScene 
{
	NSLog(@"Warning !: (addSceneWithKey) Remove me while backporting");
	[self setSceneForKey:aSceneKey Scene:aScene];
}

- (BOOL)setCurrentSceneToSceneWithKey:(NSString*)aSceneKey {
	
	NSLog(@"Warning !: (setCurrentSceneToSceneWithKey) Remove me while backporting");
	[self setActiveScene:aSceneKey];
	return YES;
}

- (BOOL)transitionToSceneWithKey:(NSString*)aSceneKey 
{
	NSLog(@"Warning !: (transitionToSceneWithKey) Remove me while backporting");
	return YES;
}

- (void) dealloc
{
	[_Scenes release];
	[super dealloc];
}



@end
